var DAO = require('./DAO')
//针对用户数据表操作的模块对象
var chatDAO = {
    // 已收藏内容
    collectAll: function (user, callback) {
        DAO('select collect.*,users.userName from users,collect where users.userId=collect.touserId and userId = (select userId from users where phone = ?)', user.phone, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //typeID==0套餐收藏图片
    lookMenuPic: function (user, callback) {
        DAO('select mealId,mealTitle,menuName,menuPic from mm join (menu,meal) on mm.tomenuId = menu.menuId and mm.tomealId = meal.mealId where mealId in(select collect.collectType from users,collect where users.userId=collect.touserId and userId = (select userId from users where phone = ?) and collect.typeId=0)', user.phone, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    //typeID==1老人收藏图片
    lookPeoplePic: function (user, callback) {
        DAO('select peopleId,healthPic,healthTitle from people where peopleId in(select collect.collectType from users,collect where users.userId=collect.touserId and userId=(select userId from users where phone = ?) and collect.typeId=1)', user.phone, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    //typeID==2分享收藏图片
    lookSharePic: function (user, callback) {
        DAO('select upmenuId,finishPic,upmenuTitle,likeNum,upTime from upmenu where upmenuId in(select collect.collectType from users,collect where users.userId=collect.touserId and userId= (select userId from users where phone = ?) and collect.typeId=2)', user.phone, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //添加套餐到收藏
    collectAdd: function (collect, callback) {
        DAO('insert into collect(collectTime,collectType,typeId,touserId) VALUES(now(),?,?,(select userId from users where phone = ?))', [collect.collectType, collect.typeId, collect.user.phone], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    lookCollectAdd: function (user, callback) {
        DAO('select collectId,collectType,typeId,touserId from collect where touserId=(select userId from users where phone = ?)', user.phone, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //删除已收藏
    collectDelete: function (collect, callback) {
        DAO('DELETE FROM collect where collectType =? and typeId =? ', [collect.collectType, collect.typeId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //已评论内容
    getComment: function (comment, callback) {
        DAO('select * from comment join users on comment.touserId=users.userId where typeId = ? and commentType = ? order by commentTime desc', [comment.typeId, comment.commentType], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //发表评论
    upComment: function (comment, callback) {
        DAO('insert into comment(commentTime,commentContent,commentType,typeId,touserId) values (now(),?,?,?,(select userId from users where phone = ?))', [comment.commentContent, comment.commentType, comment.typeId, comment.user.phone], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    delComment: function (commentId, callback) {
        DAO('delete from comment where commentId=?', commentId, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
}
module.exports = chatDAO